<?php
require_once "Connection.php";
class FacebookAPI{
	
	public function __construct() {
		
	}
	
	
	public function login(){
		$app_id = APP_ID;
        $app_secret = APP_SECRET;
        $my_url = REDIRECT_URI;
		$login_url = LOGIN_URL;
        $token_url = TOKEN_URL;
		$graph_url = GRAPH_URL;
        $code = @$_REQUEST["code"];
		
        if (empty($code)) {
            /*
             * 登入維持24小時
             */
            $expire_minute = 60 * 60 * 24;
            
            /*
             * 超過登入時效
             */
            if (isset($_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME']) && (time() - $_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME'] > $expire_minute)) {
                session_destroy(); // destroy session data in storage
                session_unset(); // unset $_SESSION variable for the runtime
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            
            }
            
            /*
             * 尚未login
             */
            
            if (!isset($_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME']) || !isset($_SESSION['LOGIN_INFO']['LOGIN_CODE'])) {
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            }
        
            
            if ( empty($code) && !isset($_SESSION['LOGIN_INFO']['LOGIN_CODE'])) {
                $dialog_url = $login_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url);
                echo("<script> top.location.href='".$dialog_url."'</script>");
				exit;
            }
        }

        
		
        
		/*
		 * 確認login code 正確
		 * 並拉回user info
		 */
		
		$code = isset($_REQUEST["code"])?$_REQUEST["code"]:@$_SESSION['LOGIN_INFO']['LOGIN_CODE'];
		
        $token_url = $token_url."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&expires_in=86400&code=".$code;
        
        $access_token = @file_get_contents($token_url);
        
        $graph_url = $graph_url.$access_token;
        
        $user = json_decode(@file_get_contents($graph_url));
        
		
		if(isset($user->id)){
			/*
			 * 確認登入成功
			 */
			$_SESSION['LOGIN_INFO']['LOGIN_CODE'] = $code;
        	$_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME'] = time();
			$_SESSION['LOGIN_INFO']['id'] = $user->id;
			$_SESSION['LOGIN_INFO']['name'] = $user->name;
			
			$conn = new Connection();
			$conn->checkIfUserSetUp($user->id);
			
		}
		else{
			$user = false;
		}
		
		
		return $user;
	}
	
	public function logout(){
		echo("<script> top.location.href='".LOGOUT_URL."'</script>");
		exit;
	}
	
	public function checkLoginStatus(){
		/*
		 * 登入維持24小時
		 */
		$expire_minute = 60*60*24;
		
		/*
		 * 超過登入時效
		 */
        if (isset($_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME']) && (time() - $_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME'] > $expire_minute)) {
            session_destroy(); // destroy session data in storage
            session_unset(); // unset $_SESSION variable for the runtime
            return false;
        }
		elseif(!isset($_SESSION['LOGIN_INFO']['LAST_LOGIN_TIME']) || !isset($_SESSION['LOGIN_INFO']['LOGIN_CODE'])){
			return false;
		}
		else{
			return true;
		}
	}
	
}


?>